package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblTipoCuenta;
import com.mastercard.assets.jpa.TblTipoCuentaReal;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.util.to.UserLogin;

public class CrearCuenta
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		System.out.println( "CrearCuenta: 1" ) ;
		
		try
		{
			System.out.println( "CrearCuenta: 2" ) ;
			
			List< TblTipoCuenta > cats = MasterCardDAO.getAllActiveCountTypes( ) ;
			
			System.out.println( "CrearCuenta: 3: " + cats.size( ) ) ;
			
			// en cao de que no haya categorias activas
			if( cats.size( ) == 0 )
			{
                            if( user.getIdioma( ) == 0 ) {
                                user.setMessageError2( MCUtil.messages.getString( "com.mastercard.assets.web.errors.modules.catalogos.cuenta.crear.NoRealCountTypeExist" ) ) ;
                            } else {
                                user.setMessageError2( MCUtil.messages.getString( "com.mastercard.assets.web.errors.modules.catalogos.cuenta.crear.NoRealCountTypeExistEng" ) ) ;
                            }
				
				
				user.setNextWorkArea( "/CalcularListaCatalogosTipoCuentas.do" ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "success" ) ;
			}
			
			List< TblTipoCuentaReal > catsReal = MasterCardDAO.getAllActiveRealCountTypes( ) ;
			
			System.out.println( "CrearCuenta: 4: " + catsReal.size( ) ) ;
			
			// en cao de que no haya categorias activas
			if( catsReal.size( ) == 0 )
			{
				if( user.getIdioma( ) == 0 ) {
                                    user.setMessageError2( MCUtil.messages.getString( "com.mastercard.assets.web.errors.modules.catalogos.cuenta.crear.NoRealCountTypeExist" ) ) ;
                                } else {
                                    user.setMessageError2( MCUtil.messages.getString( "com.mastercard.assets.web.errors.modules.catalogos.cuenta.crear.NoRealCountTypeExistEng" ) ) ;
                                }
				
				user.setNextWorkArea( "/CalcularListaCatalogosTipoCuentas.do" ) ;
				
                                MasterCardDAO.closeEntityManager( ) ;
				return mapping.findForward( "success" ) ;
			}
			
			user.setMessageError( "" ) ;
		}catch( MasterCardException e )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			e.printStackTrace( ) ;
			user.setMessageError( e.getMessage( ) ) ;
		}
		
                
                MasterCardDAO.closeEntityManager( ) ;
		// en caso de que se cumpla, guarda el WorkArea
                if( user.getIdioma( ) == 0 ) {
                    user.setNextWorkArea( "/WEB-INF/pages/WorkArea/CrearCuenta.jsp" ) ;
                } else {
                    user.setNextWorkArea( "/WEB-INF/pages/WorkAreaEng/CrearCuenta.jsp" ) ;
                }
		
		
	  	return mapping.findForward( "success" ) ;
	}
	
}
